﻿<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="TaskSchedulerV1Schema"
	targetNamespace="http://schemas.microsoft.com/windows/2004/02/mit/task"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"
	xmlns:td="http://schemas.microsoft.com/windows/2004/02/mit/task"
	elementFormDefault="qualified">

  <xs:element name="Task" type="taskType">

	<xs:key name="PrincipalKey">
	  <xs:selector xpath="td:Principals/td:Principal" />
	  <xs:field xpath="@id" />
	</xs:key>

	<!-- Principal id in Context attribute should match an id of some principal in Principals section. -->
	<xs:keyref name="ContextKeyRef" refer="PrincipalKey">
	  <xs:selector xpath="td:Actions" />
	  <xs:field xpath="@Context" />
	</xs:keyref>

	<!-- All ids must be unique -->
	<xs:unique name="UniqueId">
	  <xs:selector xpath="td:Principals/td:Principal|td:Triggers/td:BootTrigger|td:Triggers/td:IdleTrigger|td:Triggers/td:TimeTrigger|td:Triggers/td:LogonTrigger|td:Triggers/td:CalendarTrigger|td:Actions/td:Exec" />
	  <xs:field xpath="@id" />
	</xs:unique>

  </xs:element>

  <xs:simpleType name="nonEmptyString">
	<xs:restriction base="xs:string">
	  <xs:minLength value="1"/>
	</xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="pathType">
	<xs:restriction base="nonEmptyString">
	  <xs:maxLength value="260"/>
	</xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="versionType">
	<xs:restriction base="xs:string">
	  <xs:pattern value="\d+(\.\d+){1,3}" />
	</xs:restriction>
  </xs:simpleType>

  <!-- Task -->
  <xs:complexType name="taskType">
	<xs:all>
	  <xs:element name="RegistrationInfo" type="registrationInfoType" minOccurs="0" />
	  <xs:element name="Triggers"         type="triggersType"         minOccurs="0" />
	  <xs:element name="Settings"         type="settingsType"         minOccurs="0" />
	  <xs:element name="Data"             type="dataType"             minOccurs="0" />
	  <xs:element name="Principals"       type="principalsType"       minOccurs="0" />
	  <xs:element name="Actions"          type="actionsType" />
	</xs:all>
	<xs:attribute name="version" use="optional" fixed="1.1" type="versionType" />
  </xs:complexType>

  <!-- RegistrationInfo -->
  <xs:complexType name="registrationInfoType">
	<xs:all>
		<xs:element name="URI"                type="xs:anyURI"                  minOccurs="0" />
		<xs:element name="SecurityDescriptor" type="xs:string"                  minOccurs="0" />
		<xs:element name="Source"             type="xs:string"                  minOccurs="0" />
		<xs:element name="Date"               type="xs:dateTime"                minOccurs="0" />
		<xs:element name="Author"             type="xs:string"                  minOccurs="0" />
		<xs:element name="Version"            type="xs:string"                  minOccurs="0" />
		<xs:element name="Description"        type="xs:string"                  minOccurs="0" />
		<xs:element name="Documentation"      type="xs:string"                  minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- Triggers -->
  <xs:complexType name="triggersType">
	<xs:group ref="triggerGroup" minOccurs="0" maxOccurs="48"/>
  </xs:complexType>
  
  <xs:group name="triggerGroup">
	<xs:choice>
	  <xs:element name="BootTrigger"               type="bootTriggerType"               minOccurs="0" />
	  <xs:element name="IdleTrigger"               type="idleTriggerType"               minOccurs="0" />
	  <xs:element name="TimeTrigger"               type="timeTriggerType"               minOccurs="0" />
	  <xs:element name="LogonTrigger"              type="logonTriggerType"              minOccurs="0" />
	  <xs:element name="CalendarTrigger"           type="calendarTriggerType"           minOccurs="0" />
	</xs:choice>
  </xs:group>

  <!-- Base type for all triggers -->
  <xs:complexType name="triggerBaseType" abstract="true">
	<xs:sequence>
	  <xs:element name="Enabled"            type="xs:boolean"     default="true"  minOccurs="0" />
	  <xs:element name="StartBoundary"      type="xs:dateTime"                    minOccurs="0" />
	  <xs:element name="EndBoundary"        type="xs:dateTime"                    minOccurs="0" />
	  <xs:element name="Repetition"         type="repetitionType"                 minOccurs="0" />
	</xs:sequence>
  </xs:complexType>

  <!-- Repetition -->
  <xs:complexType name="repetitionType">
	<xs:all>
	  <xs:element name="Interval">
		<xs:simpleType>
		  <xs:restriction base="xs:duration">
			<xs:minInclusive value="PT1M"/>
			<xs:maxInclusive value="P31D"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	  <xs:element name="Duration" minOccurs="0">
		<xs:simpleType>
		  <xs:restriction base="xs:duration">
			<xs:minInclusive value="PT1M"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	  <xs:element name="StopAtDurationEnd"    type="xs:boolean" default="false" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- BootTrigger -->
  <xs:complexType name="bootTriggerType">
	<xs:complexContent>
	  <xs:extension base="triggerBaseType" />
	</xs:complexContent>
  </xs:complexType>

  <!-- IdleTrigger -->
  <xs:complexType name="idleTriggerType">
	<xs:complexContent>
	  <xs:extension base="triggerBaseType" />
	</xs:complexContent>
  </xs:complexType>

  <!-- TimeTrigger -->
  <xs:complexType name="timeTriggerType">
	<xs:complexContent>
	  <xs:extension base="triggerBaseType" />
	</xs:complexContent>
  </xs:complexType>

  <!-- LogonTrigger -->
  <xs:complexType name="logonTriggerType">
	<xs:complexContent>
	  <xs:extension base="triggerBaseType" />
	</xs:complexContent>
  </xs:complexType>

  <!-- CalendarTrigger -->
  <xs:complexType name="calendarTriggerType">
	<xs:complexContent>
	  <xs:extension base="triggerBaseType">
		<xs:sequence>
		  <xs:choice>
			<xs:element name="ScheduleByDay"            type="dailyScheduleType" />
			<xs:element name="ScheduleByWeek"           type="weeklyScheduleType" />
			<xs:element name="ScheduleByMonth"          type="monthlyScheduleType" />
			<xs:element name="ScheduleByMonthDayOfWeek" type="monthlyDayOfWeekScheduleType" />
		  </xs:choice>
		</xs:sequence>
	  </xs:extension>
	</xs:complexContent>
  </xs:complexType>

  <!-- DailySchedule -->
  <xs:complexType name="dailyScheduleType">
	<xs:all>
	  <xs:element name="DaysInterval" minOccurs="0">
		<xs:simpleType>
		  <xs:restriction base="xs:unsignedInt">
			<xs:minInclusive value="1"/>
			<xs:maxInclusive value="365"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	</xs:all>
  </xs:complexType>

  <!-- WeeklySchedule -->
  <xs:complexType name="weeklyScheduleType">
	<xs:all>
	  <xs:element name="WeeksInterval" minOccurs="0">
		<xs:simpleType>
		  <xs:restriction base="xs:unsignedByte">
			<xs:minInclusive value="1"/>
			<xs:maxInclusive value="52"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	  <xs:element name="DaysOfWeek" type="daysOfWeekType" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- MonthlySchedule -->
  <xs:complexType name="monthlyScheduleType">
	<xs:all>
	  <xs:element name="DaysOfMonth" type="daysOfMonthType" minOccurs="0" />
	  <xs:element name="Months" type="monthsType" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- MonthlyDayOfWeekSchedule -->
  <xs:complexType name="monthlyDayOfWeekScheduleType">
	<xs:all>
	  <xs:element name="Weeks"        type="weeksType" minOccurs="0" />
	  <xs:element name="DaysOfWeek"   type="daysOfWeekType" />
	  <xs:element name="Months"       type="monthsType" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- DaysOfWeek -->
  <xs:complexType name="daysOfWeekType">
	<xs:all>
	  <xs:element name="Monday"       fixed="" minOccurs="0" />
	  <xs:element name="Tuesday"      fixed="" minOccurs="0" />
	  <xs:element name="Wednesday"    fixed="" minOccurs="0" />
	  <xs:element name="Thursday"     fixed="" minOccurs="0" />
	  <xs:element name="Friday"       fixed="" minOccurs="0" />
	  <xs:element name="Saturday"     fixed="" minOccurs="0" />
	  <xs:element name="Sunday"       fixed="" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- Months -->
  <xs:complexType name="monthsType">
	<xs:all>
	  <xs:element name="January"      fixed="" minOccurs="0" />
	  <xs:element name="February"     fixed="" minOccurs="0" />
	  <xs:element name="March"        fixed="" minOccurs="0" />
	  <xs:element name="April"        fixed="" minOccurs="0" />
	  <xs:element name="May"          fixed="" minOccurs="0" />
	  <xs:element name="June"         fixed="" minOccurs="0" />
	  <xs:element name="July"         fixed="" minOccurs="0" />
	  <xs:element name="August"       fixed="" minOccurs="0" />
	  <xs:element name="September"    fixed="" minOccurs="0" />
	  <xs:element name="October"      fixed="" minOccurs="0" />
	  <xs:element name="November"     fixed="" minOccurs="0" />
	  <xs:element name="December"     fixed="" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- DaysOfMonth -->
  <xs:complexType name="daysOfMonthType">
	<xs:sequence>
	  <xs:element name="Day" type="dayOfMonthType" minOccurs="0" maxOccurs="32" />
	</xs:sequence>
  </xs:complexType>
  <xs:simpleType name="dayOfMonthType">
	<xs:restriction base="xs:string">
	  <xs:pattern value="[1-9]|[1-2][0-9]|3[0-1]|Last" />
	</xs:restriction>
  </xs:simpleType>

  <!-- Weeks -->
  <xs:complexType name="weeksType">
	<xs:sequence>
	  <xs:element name="Week" type="weekType" minOccurs="0" maxOccurs="5" />
	</xs:sequence>
  </xs:complexType>
  <xs:simpleType name="weekType">
	<xs:restriction base="xs:string">
	  <xs:pattern value="[1-4]|Last" />
	</xs:restriction>
  </xs:simpleType>

  <!-- Settings -->
  <xs:complexType name="settingsType">
	<xs:all>
	  <xs:element name="DisallowStartIfOnBatteries"       type="xs:boolean"                   default="true"      minOccurs="0" />
	  <xs:element name="StopIfGoingOnBatteries"           type="xs:boolean"                   default="true"      minOccurs="0" />
	  <xs:element name="RunOnlyIfNetworkAvailable"        type="xs:boolean"                   default="false"     minOccurs="0" />
	  <xs:element name="WakeToRun"                        type="xs:boolean"                   default="false"     minOccurs="0" />
	  <xs:element name="Enabled"                          type="xs:boolean"                   default="true"      minOccurs="0" />
	  <xs:element name="Hidden"                           type="xs:boolean"                   default="false"     minOccurs="0" />
	  <xs:element name="DeleteExpiredTaskAfter"           type="xs:duration"                  default="PT0S"      minOccurs="0" />
	  <xs:element name="IdleSettings"                     type="idleSettingsType"                                 minOccurs="0" />
	  <xs:element name="ExecutionTimeLimit"               type="xs:duration"                  default="PT72H"     minOccurs="0" />
	  <xs:element name="Priority"                         type="priorityType"                 default="7"         minOccurs="0" />
	  <xs:element name="RunOnlyIfIdle"                    type="xs:boolean"                   default="false"     minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- Lower number means higher priority. -->
  <xs:simpleType name="priorityType">
	<xs:restriction base="xs:byte">
	  <xs:minInclusive value="0" fixed="true" />
	  <xs:maxInclusive value="10" fixed="true" />
	</xs:restriction>
  </xs:simpleType>

  <!-- IdleSettings -->
  <xs:complexType name="idleSettingsType">
	<xs:all>
	  <xs:element name="Duration" default="PT10M" minOccurs="0">
		<xs:simpleType>
		  <xs:restriction base="xs:duration">
			<xs:minInclusive value="PT1M"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	  <xs:element name="WaitTimeout" default="PT1H"  minOccurs="0">
		<xs:simpleType>
		  <xs:restriction base="xs:duration">
			<xs:minInclusive value="PT1M"/>
		  </xs:restriction>
		</xs:simpleType>
	  </xs:element>
	  <xs:element name="StopOnIdleEnd" type="xs:boolean"  default="true"  minOccurs="0" />
	  <xs:element name="RestartOnIdle" type="xs:boolean"  default="false" minOccurs="0" />
	</xs:all>
  </xs:complexType>

  <!-- Data -->
  <xs:complexType name="dataType">
	<xs:sequence>
	  <xs:any />
	</xs:sequence>
  </xs:complexType>

  <!-- Principals -->
  <xs:complexType name="principalsType">
	<xs:sequence>
	  <xs:element name="Principal" type="principalType" maxOccurs="1" />
	</xs:sequence>
  </xs:complexType>

  <!-- Principal -->
  <xs:complexType name="principalType">
	<xs:all>
	  <xs:element name="UserId" type="nonEmptyString" minOccurs="0" />
	  <xs:element name="LogonType" type="logonType" minOccurs="0" maxOccurs="1"/>
	</xs:all>
  </xs:complexType>
  
  <xs:simpleType name="logonType">
	<xs:restriction base="xs:string">
	  <xs:enumeration value="Password" />
	  <xs:enumeration value="InteractiveToken" />
	  <xs:enumeration value="InteractiveTokenOrPassword" />
	</xs:restriction>
  </xs:simpleType>

  <!-- Actions -->
  <xs:complexType name="actionsType">
	<xs:sequence>
	  <xs:group ref="actionGroup" maxOccurs="1" />
	</xs:sequence>
	<xs:attribute name="Context" type="xs:IDREF" use="optional" />
  </xs:complexType>

  <xs:group name="actionGroup">
	  <xs:choice>
		  <xs:element name="Exec"          type="execType" />
		  <xs:element name="ComHandler"    type="comHandlerType" />
		  <xs:element name="SendEmail"     type="sendEmailType" />
		  <xs:element name="ShowMessage"   type="showMessageType" />
	  </xs:choice>
  </xs:group>

  <!-- Base type for actions. -->
  <xs:complexType name="actionBaseType" abstract="true">
	  <xs:attribute name="id" type="xs:ID" use="optional" />
  </xs:complexType>

  <!-- Exec -->
  <xs:complexType name="execType">
	<xs:complexContent>
	  <xs:extension base="actionBaseType">
		<xs:all>
		  <xs:element name="Command"          type="pathType"/>
		  <xs:element name="Arguments"        type="xs:string" minOccurs="0"/>
		  <xs:element name="WorkingDirectory" type="pathType" minOccurs="0"/>
		</xs:all>
	  </xs:extension>
	</xs:complexContent>
  </xs:complexType>

	<!-- ComHandler -->
	<xs:complexType name="comHandlerType">
		<xs:complexContent>
			<xs:extension base="actionBaseType">
				<xs:all>
					<xs:element name="ClassId"  type="guidType" />
					<xs:element name="Data"     type="dataType" minOccurs="0" />
				</xs:all>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>

	<xs:simpleType name="guidType">
		<xs:restriction base="xs:string">
			<!-- GUID should be in a form: {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} where x is a hexadecimal digit. -->
			<xs:pattern value="[\{]?([0-9a-fA-F]){8}(\-[0-9a-fA-F]{4}){3}\-[0-9a-fA-F]{12}[\}]?" />
		</xs:restriction>
	</xs:simpleType>

	<!-- SendEmail -->
	<xs:complexType name="sendEmailType">
		<xs:complexContent>
			<xs:extension base="actionBaseType">
				<xs:all>
					<xs:element name="Server"       type="nonEmptyString" />
					<xs:element name="Subject"      type="xs:string"        minOccurs="0" />
					<xs:element name="To"           type="xs:string"        minOccurs="0" />
					<xs:element name="Cc"           type="xs:string"        minOccurs="0" />
					<xs:element name="Bcc"          type="xs:string"        minOccurs="0" />
					<xs:element name="ReplyTo"      type="xs:string"        minOccurs="0" />
					<xs:element name="From"         type="xs:string"        minOccurs="0" />
					<xs:element name="HeaderFields" type="headerFieldsType" minOccurs="0" />
					<xs:element name="Body"         type="xs:string"        minOccurs="0" />
					<xs:element name="Attachments"  type="attachmentsType"  minOccurs="0" />
				</xs:all>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>

	<xs:complexType name="headerFieldsType">
		<xs:sequence>
			<xs:element name="HeaderField"  type="headerFieldType"  minOccurs="0"   maxOccurs="32"/>
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="headerFieldType">
		<xs:all>
			<xs:element name="Name"         type="nonEmptyString" />
			<xs:element name="Value"        type="xs:string" />
		</xs:all>
	</xs:complexType>

	<xs:complexType name="attachmentsType">
		<xs:sequence>
			<xs:element name="File"  type="nonEmptyString"  minOccurs="0"   maxOccurs="8"/>
		</xs:sequence>
	</xs:complexType>

	<!-- ShowMessage -->
	<xs:complexType name="showMessageType">
		<xs:complexContent>
			<xs:extension base="actionBaseType">
				<xs:all>
					<xs:element name="Title"  type="nonEmptyString" />
					<xs:element name="Body"   type="nonEmptyString" />
				</xs:all>
			</xs:extension>
		</xs:complexContent>
	</xs:complexType>

</xs:schema>